//
//  ColorItem.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/12.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "ColorItem.h"

@interface ColorItem()

@property (strong, nonatomic) UIView *centerView;
@property (strong, nonatomic) CALayer *shadowLayer;

@end

@implementation ColorItem

- (instancetype)initWithFrame:(CGRect)frame color:(UIColor *)color
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        
        self.centerView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, frame.size.width - 20, frame.size.height - 20)];
        self.centerView.backgroundColor = color;
        self.centerView.layer.cornerRadius = self.centerView.height / 2;
        self.centerView.clipsToBounds = YES;
        self.centerView.layer.borderWidth = 2;
        self.centerView.layer.borderColor = [[UIColor colorWithHexString:@"#707070"] CGColor];
        [self addSubview:self.centerView];
        [self addShadowToView:self.centerView withOpacity:0.7 shadowRadius:10 andCornerRadius:self.centerView.height / 2];
        self.centerView.userInteractionEnabled = NO;
        
        self.shadowLayer.hidden = YES;
    }
    return self;
}

- (void)enlarge{
    self.centerView.frame = CGRectMake(5, 5, self.frame.size.width - 10, self.frame.size.height - 10);
    self.centerView.layer.cornerRadius = self.centerView.height / 2;
    self.centerView.layer.borderWidth = 5;
    self.centerView.layer.borderColor = [[UIColor whiteColor] CGColor];
    self.shadowLayer.hidden = NO;
}

- (void)narrow{
    self.centerView.frame = CGRectMake(10, 10, self.frame.size.width - 20, self.frame.size.height - 20);
    self.centerView.layer.cornerRadius = self.centerView.height / 2;
    self.centerView.layer.borderWidth = 2;
    self.centerView.layer.borderColor = [[UIColor colorWithHexString:@"#707070"] CGColor];
    self.shadowLayer.hidden = YES;
}

- (void)addShadowToView:(UIView *)view
            withOpacity:(float)shadowOpacity
           shadowRadius:(CGFloat)shadowRadius
        andCornerRadius:(CGFloat)cornerRadius
{
    //////// shadow /////////
    self.shadowLayer = [CALayer layer];
    self.shadowLayer.frame = view.layer.frame;
    
    self.shadowLayer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
    self.shadowLayer.shadowOffset = CGSizeMake(0, 0);//shadowOffset阴影偏移，默认(0, -3),这个跟shadowRadius配合使用
    self.shadowLayer.shadowOpacity = shadowOpacity;//0.8;//阴影透明度，默认0
    self.shadowLayer.shadowRadius = shadowRadius;//8;//阴影半径，默认3
    
    //路径阴影
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    float width = self.shadowLayer.bounds.size.width;
    float height = self.shadowLayer.bounds.size.height;
    float x = self.shadowLayer.bounds.origin.x;
    float y = self.shadowLayer.bounds.origin.y;
    
    CGPoint topLeft      = self.shadowLayer.bounds.origin;
    CGPoint topRight     = CGPointMake(x + width, y);
    CGPoint bottomRight  = CGPointMake(x + width, y + height);
    CGPoint bottomLeft   = CGPointMake(x, y + height);
    
    CGFloat offset = -1.f;
    [path moveToPoint:CGPointMake(topLeft.x - offset, topLeft.y + cornerRadius)];
    [path addArcWithCenter:CGPointMake(topLeft.x + cornerRadius, topLeft.y + cornerRadius) radius:(cornerRadius + offset) startAngle:M_PI endAngle:M_PI_2 * 3 clockwise:YES];
    [path addLineToPoint:CGPointMake(topRight.x - cornerRadius, topRight.y - offset)];
    [path addArcWithCenter:CGPointMake(topRight.x - cornerRadius, topRight.y + cornerRadius) radius:(cornerRadius + offset) startAngle:M_PI_2 * 3 endAngle:M_PI * 2 clockwise:YES];
    [path addLineToPoint:CGPointMake(bottomRight.x + offset, bottomRight.y - cornerRadius)];
    [path addArcWithCenter:CGPointMake(bottomRight.x - cornerRadius, bottomRight.y - cornerRadius) radius:(cornerRadius + offset) startAngle:0 endAngle:M_PI_2 clockwise:YES];
    [path addLineToPoint:CGPointMake(bottomLeft.x + cornerRadius, bottomLeft.y + offset)];
    [path addArcWithCenter:CGPointMake(bottomLeft.x + cornerRadius, bottomLeft.y - cornerRadius) radius:(cornerRadius + offset) startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
    [path addLineToPoint:CGPointMake(topLeft.x - offset, topLeft.y + cornerRadius)];
    
    //设置阴影路径
    self.shadowLayer.shadowPath = path.CGPath;
    
    //////// cornerRadius /////////
    view.layer.cornerRadius = cornerRadius;
    view.layer.masksToBounds = YES;
    view.layer.shouldRasterize = YES;
    view.layer.rasterizationScale = [UIScreen mainScreen].scale;
    
    [view.superview.layer insertSublayer:self.shadowLayer below:view.layer];
}

@end
